package com.uchengwang.functions.image;

/**
 * Created by U城网 on 2017/3/3.
 */

import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;

import com.bumptech.glide.DrawableTypeRequest;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.request.target.SimpleTarget;
import com.uchengwang.functions.glide.GlideCircleTransform;
import com.uchengwang.functions.glide.GlideRoundTransform;
import com.uchengwang.ui.utils.ExecutorsUtils;

import java.io.File;

/**
 * fresco图片处理类
 * */
public class ImageManager {

    public enum ImageType{
        CIRCLE,
        ROUND,
        NARMOL
    }
    private int roundSize = 10;

    private ImageManager(){}

    private static ImageManager imageManager;
    public static ImageManager getInstance() {
        if (imageManager == null){
            imageManager = new ImageManager();
        }
        return imageManager;
    }

    //当设置图片有圆角时，先设置值
    public ImageManager setRoundSize(int size){
        roundSize = size;
        return this;
    }

    //加载网络图片
    public void loadImage(Context con , ImageView view, String url, ImageType type){
//        DrawableTypeRequest request = Glide.with(con).load(url);
//        request.diskCacheStrategy(DiskCacheStrategy.ALL);//设置缓存策略
//        request.thumbnail(0.1f);
//        request.crossFade();//淡入淡出,注意:如果设置了这个,则必须要去掉asBitmap
//        switch (type){
//            case CIRCLE:
//                request.transform(new GlideCircleTransform(con)).into(view);
//                break;
//            case ROUND:
//                request.transform(new CenterCrop(con), new GlideRoundTransform(con, roundSize)).into(view);
//                break;
//            case NARMOL:
//                request.into(view);
//                break;
//        }
        loadImage(con, view, 0, 0, url, type);
    }

    //加载网络图片
    public void loadImage(Context con , ImageView view, int w, int h,  String url, ImageType type){
        DrawableTypeRequest request = Glide.with(con.getApplicationContext()).load(url);
        request.diskCacheStrategy(DiskCacheStrategy.ALL);//设置缓存策略
        request.thumbnail(0.5f);
        request.crossFade();//淡入淡出,注意:如果设置了这个,则必须要去掉asBitmap
        if (w >0 && h >0){
            request.override(w, h);
        }
//        request.skipMemoryCache(true);//设置跳过内存缓存 这意味着 Glide 将不会把这张图片放到内存缓存中去 这里需要明白的是，这只是会影响内存缓存！Glide 将会仍然利用磁盘缓存来避免重复的网络请求。
        switch (type){
            case CIRCLE:
                request.transform(new GlideCircleTransform(con.getApplicationContext())).into(view);
                break;
            case ROUND:
                request.transform(new CenterCrop(con.getApplicationContext()), new GlideRoundTransform(con.getApplicationContext(), roundSize)).into(view);
                break;
            case NARMOL:
                request.into(view);
                break;
        }
    }

    //加载资源文件
    public void loadImage(Context con, ImageView view, int res, ImageType type){
//        DrawableTypeRequest request = Glide.with(con).load(res);
//        request.crossFade();//淡入淡出,注意:如果设置了这个,则必须要去掉asBitmap
//        switch (type){
//            case CIRCLE:
//                request.transform(new GlideCircleTransform(con)).into(view);
//                break;
//            case ROUND:
//                request.transform(new CenterCrop(con), new GlideRoundTransform(con, roundSize)).into(view);
//                break;
//            case NARMOL:
//                request.into(view);
//                break;
//        }
        loadImage(con, view, 0, 0, res, type);
    }

    public void loadImage(Context con, ImageView view, int w, int h,  int res, ImageType type){
        DrawableTypeRequest request = Glide.with(con.getApplicationContext()).load(res);
        request.crossFade();//淡入淡出,注意:如果设置了这个,则必须要去掉asBitmap
        if (w >0 && h >0){
            request.override(w, h);
        }
        switch (type){
            case CIRCLE:
                request.transform(new GlideCircleTransform(con.getApplicationContext())).into(view);
                break;
            case ROUND:
                request.transform(new CenterCrop(con.getApplicationContext()), new GlideRoundTransform(con.getApplicationContext(), roundSize)).into(view);
                break;
            case NARMOL:
                request.into(view);
                break;
        }
    }

    //加载本地文件
    public void loadImage(Context con, ImageView view, File file, ImageType type){
        DrawableTypeRequest request = Glide.with(con.getApplicationContext()).load(file);
//        request.diskCacheStrategy(DiskCacheStrategy.ALL);//设置缓存策略
        request.crossFade();//淡入淡出,注意:如果设置了这个,则必须要去掉asBitmap
        switch (type){
            case CIRCLE:
                request.transform(new GlideCircleTransform(con.getApplicationContext())).into(view);
                break;
            case ROUND:
                request.transform(new CenterCrop(con.getApplicationContext()), new GlideRoundTransform(con.getApplicationContext(), roundSize)).into(view);
                break;
            case NARMOL:
                request.into(view);
                break;
        }
    }

    /**
     * 宽高缩放
     * */
    public void scale(){



    }

    /**
     * 根据url获取图片bitmap
     * */
    public void getBitmap(Context context, String url, SimpleTarget<Bitmap> simpleTarget){
        Glide.with(context).load(url).asBitmap().into(simpleTarget); //方法中设置asBitmap可以设置回调类型

    }

    /**
     * 清除磁盘缓存
     * */
    public void clearGlideDiskCache(final Context context){
        ExecutorsUtils.getUtils().createSingleThread().execute(new Runnable() {
            @Override
            public void run() {
                Glide.get(context.getApplicationContext()).clearDiskCache();//清理磁盘缓存 需要在子线程中执行
            }
        });
    }

    /**
     * 清除内存缓存
     * */
    public void clearGlideMemory(Context context){
        Glide.get(context.getApplicationContext()).clearMemory();//清理内存缓存  可以在UI主线程中进行
    }
}
